﻿package client;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JRadioButton;

import server.ComHouseInterface;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import client.Configuration;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;



public class ComComeinGUI extends JDialog {

	private JPanel contentPane;
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;


	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ComComeinGUI frame = new ComComeinGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 */
	public ComComeinGUI() {
		setTitle("\u4F4F\u6237\u767B\u9646");
		setModalityType(ModalityType.APPLICATION_MODAL);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setToolTipText("");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Key\uFF1A");
		lblNewLabel.setBounds(64, 80, 43, 22);
		contentPane.add(lblNewLabel);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(117, 81, 125, 21);
		contentPane.add(passwordField);
		
		JButton btnNewButton = new JButton("Open Door");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String hN = passwordField_1.getText();
				int hn = Integer.parseInt(hN);
				
				String passwordtext = passwordField.getText();
				String str = new String(passwordtext);
				
			
				try {
					
					Configuration config = new Configuration();
					String rmiAddress=config.getRmiAddress();
					ComHouseInterface comHouse = (ComHouseInterface) Naming.lookup(rmiAddress+"ComHouseList"); 
					String k = comHouse.getKey(hn);
					if((str.equals(k)) == true){
						ComHouseGUI chg = new ComHouseGUI();
						chg.setLocationRelativeTo(null);
						chg.setVisible(true);
					    dispose();
					}
					else{
						passwordField.setText("");
						passwordField_1.setText("");
					    JOptionPane.showMessageDialog(null, "您的信息填写有误，请重新填写！");
					}
				} catch (MalformedURLException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				} catch (RemoteException e3) {
					// TODO Auto-generated catch block
					e3.printStackTrace();
				} catch (NotBoundException e4) {
					// TODO Auto-generated catch block
					e4.printStackTrace();
				}
				
			}
		});
		btnNewButton.setBounds(117, 134, 125, 37);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u8FD4\u56DE");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				ComLoadGUI clg = new ComLoadGUI();
				clg.setLocationRelativeTo(null);
				clg.setVisible(true);
		
			}
		});
		btnNewButton_1.setBounds(294, 207, 93, 23);
		contentPane.add(btnNewButton_1);
		
		JLabel lblNewLabel_1 = new JLabel("houseNum\uFF1A");
		lblNewLabel_1.setBounds(32, 29, 75, 27);
		contentPane.add(lblNewLabel_1);
		
		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(117, 32, 125, 21);
		contentPane.add(passwordField_1);
	}
}
